package com.yatuhashi.manual.mhp3.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper {
    private static DataBaseHelper dbHelper;
    private static final String DB_NAME = "gm_mhp3.db";
    private static final int DB_VERSION = 1;
    @SuppressWarnings("unused")
    private static Context c;
    
    @SuppressWarnings("static-access")
    public DataBaseHelper(Context c) {
        super(c, DB_NAME, null, DB_VERSION);
        this.c = c;
    }
    
    public static DataBaseHelper getInstance(Context c) {
        if(dbHelper==null) {
            dbHelper = new DataBaseHelper(c);
        }
        return dbHelper;
    }
    
    public DataBaseHelper(Context c, String name, CursorFactory factory, int ver) {
        super(c, name, factory, ver);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    	createQuestTbl(db);
    	createQuestDetailTbl(db);
    	createMonsterTbl(db);
    }
    
    private void createQuestTbl(SQLiteDatabase db) {
        StringBuffer sql = new StringBuffer();
        sql.append("create table quest_table (");
        sql.append(" id integer primary key autoincrement");
        sql.append(", type text");
        sql.append(", star text");
        sql.append(", title text");
        sql.append(", tid integer");
        sql.append(", qid integer);");
        db.execSQL(sql.toString());
    }
    
    private void createQuestDetailTbl(SQLiteDatabase db) {
    	StringBuffer sql = new StringBuffer();
    	sql.append("create table quest_detail_table (");
    	sql.append(" id integer primary key autoincrement");
    	sql.append(", qid integer");
    	sql.append(", target text");
    	sql.append(", reward text");
    	sql.append(", deposit text");
    	sql.append(", monster text");
    	sql.append(", habitat text");
    	sql.append(", limittime text");
    	sql.append(", mid integer);");
    	db.execSQL(sql.toString());
    }
    
    private void createMonsterTbl(SQLiteDatabase db) {
        StringBuffer sql = new StringBuffer();
        sql.append("create table monster_table (");
        sql.append(" id integer primary key autoincrement");
        sql.append(", name text");
        sql.append(", alias text");
        sql.append(", race text");
        sql.append(", rid integer");
        sql.append(", weakness text");
        sql.append(", mid integer);");
        db.execSQL(sql.toString());
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
    
//    public static void createDb(Context c) {
//        if(!dbExist()) {
//            getInstance(c);
//            SQLiteDatabase db = dbHelper.getReadableDatabase();
//            db.close();
//            
//            @SuppressWarnings("unused")
//            String dbFile = c.getDatabasePath(DB_NAME).getPath();
//        }
//    }
//    
//    /**
//     * DataBaseが以前作成されているかどうか
//     * @return boolean
//     */
//    private static boolean dbExist() {
//        SharedPreferences pref = c.getSharedPreferences(DataBaseConsts.DB_LOCAL, Context.MODE_PRIVATE);
//        return pref.getBoolean(DataBaseConsts.DB_CREATED, false);
//    }
}
